﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void game()
{
	int ret = rand() % 100 + 1;
	printf("游戏开始，");
	int left = 1, right = 100;
	int guess = 0;
	while (1)
	{
		printf("请输入一个合法的数字(%d~%d)：", left, right);
		scanf("%d", &guess);
		if (guess < ret)
		{
			printf("猜小了,");
			left = guess + 1;
		}
		else if (guess > ret)
		{
			printf("猜大了,");
			right = guess - 1;
		}
		else
		{
			printf("猜对了,炸弹是%d。\n", ret);
			printf("游戏结束,");
			break;
		}
	}
}

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	printf("数字炸弹：\n");
	printf("在1~100内，其中一个数字定为炸弹，猜中这个数，即为输。\n");

	do
	{
		printf("输入“1”开始游戏，或输入“0”退出游戏:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("游戏已成功退出，感谢游玩！\n");
			break;
		default:
			printf("输入错误，请重新输入一个合法的数字\n");
			break;
		}
	} while (input);

	return 0;
}